package com.hhh.Controller;

import com.hhh.Pojo.AddressBook;
import com.hhh.Pojo.Result;
import com.hhh.Service.AddressBookService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    //    根据user id查询地址列表
//    根据user id查询地址列表
//    根据user id查询地址列表
    @GetMapping("/list")
    public Result SelectList(HttpServletRequest request) {
        long id = (long) request.getSession().getAttribute("user");
        log.info("根据"+id+"查询地址列表");
        return Result.success(addressBookService.SelectList(id));
    }

    //    修改默认地址
//    修改默认地址
//    修改默认地址
    @PutMapping("/default")
    public Result IsDefault(@RequestBody AddressBook addressBook) {
        log.info("修改默认地址");
        int num = addressBookService.IsDefault(addressBook);
        if (num == 2)
            return Result.success(2);
        else
            return Result.error("修改失败");
    }

    //    添加地址
//    添加地址
//    添加地址
    @PostMapping()
    public Result Insert(@RequestBody AddressBook addressBook, HttpServletRequest request) {
        log.info("添加地址");
        addressBook.setUserId((Long) request.getSession().getAttribute("user"));
        return Result.success(addressBookService.Insert(addressBook));
    }

    //    根据AddressBook id查询地址
    @GetMapping("/{id}")
    public Result SelectById(@PathVariable long id) {
        return Result.success(addressBookService.SelectById(id));
    }

    //    修改地址
//    修改地址
//    修改地址
    @PutMapping()
    public Result UpDate(@RequestBody AddressBook addressBook) {
        log.info("修改地址");
        return Result.success(addressBookService.UpDate(addressBook));
    }

//    删除地址
//    删除地址
//    删除地址
    @DeleteMapping()
    public Result Delete(long ids) {
        log.info("删除地址");
        return Result.success(addressBookService.Delete(ids));
    }

    //    获取默认地址
//    获取默认地址
//    获取默认地址
    @GetMapping("/default")
    public Result GetDefaultAddress()
    {
        log.info("获取默认地址");
        return  Result.success(addressBookService.GetDefaultAddress());
    }

}
